/*
 * Copyright 2007-2009 Taglab Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.taglab.chic.mvc.spring;

import org.springframework.web.servlet.view.JstlView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 *
 * @author Markus Kobler
 */
public class TemplatedJstlView extends JstlView {

  public final static String TEMPLATE_KEY = "template";

  public final static String CONTENT_KEY = "content";

  public TemplatedJstlView() {
  }

  public TemplatedJstlView(String url) {
    super(url);
  }

  protected String prepareForRendering(HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {

    request.setAttribute(CONTENT_KEY, getUrl());

    // Lookup tempate
    String tempatePath = (String)request.getAttribute(TEMPLATE_KEY);

    if( tempatePath == null ) {
      tempatePath = (String) getAttributesMap().get(TEMPLATE_KEY);

      if (tempatePath == null) {
        throw new IllegalStateException("Template attribute '" + TEMPLATE_KEY + "' must be set");
      }
    }

    return tempatePath;
  }

}
